package ui;

import java.awt.Color;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

import data.Connect;

import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class UserTransfer extends JPanel {

	JTable activityTable;
	JScrollPane scroll;
	JTextField textfieldAmount;
	JButton buttonConfirm;
	JPasswordField passwordfieldConfirm;
	public String found;
	public final JTextField searchField;
	private void TransferAmount(String UserID){
				
	}
	
	public void UserSearch(String query){

	
		// USER TABLE INITIATED DURING SEARCH
		
	    DefaultTableModel userModel = new DefaultTableModel() {
	        // DISABLES EDITABLE
	    	@Override
	    	public boolean isCellEditable(int row, int column)
	        {
	          return false;//This causes all cells to be not editable
	        }
	        // CHANGES THE COLUMN CLASS IF COLUMN = 0 WHICH IS AN IMAGE/ICON
	        @Override
            public Class getColumnClass(int column)
            {
                if (column == 0) return ImageIcon.class; 
                return Object.class;
            }
        };
	    userModel.addColumn(" ");
	    userModel.addColumn("Name");
	    	    
	    // LOOPING THROUGH OUR USER ARRAY TO CREATE ROWS		    
	    Connect connect = new Connect();
	    for(data.User user : connect.searchUsers(query)){
	      				
	    	userModel.insertRow(0, new Object[] {
	    			new ImageIcon("gfx/"+user.getAvatar()),
					" " + user.getFirstName() + " " + user.getLastName(),				
			});
		}
	    if(scroll!=null){
	    	this.remove(scroll);
	    }
	    activityTable = new JTable(userModel);
	    
	    activityTable.setFont(new Font("Arial", Font.PLAIN, 12));
		activityTable.setBounds(10,220,630,180);
		activityTable.setRowHeight(74);
	    
		// SETTING WIDTH ON COLUMNS
		activityTable.getColumn(" ").setPreferredWidth(80);
		activityTable.getColumn("Name").setPreferredWidth(570);
		
		
		//activityTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);       
		
		activityTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
		    
			
				public void valueChanged(ListSelectionEvent e) {
	
					if (! e.getValueIsAdjusting())
					{
			        String selectedData = null;

			        int[] selectedRow = activityTable.getSelectedRows();
					int[] selectedColumns = activityTable.getSelectedColumns();
					
					for (int i = 0; i < selectedRow.length; i++) {
						for (int j = 0; j < selectedColumns.length; j++) {
							selectedData = (String) activityTable.getValueAt(selectedRow[i], selectedColumns[j]);
						}
					}
					TransferAmount(selectedData);

					System.out.println("Selected: " + selectedData);
					found = selectedData;
					}
				};
				
		    });
		
	    scroll = new JScrollPane(activityTable);
		scroll.setBounds(10,200,630,180);
	    add(scroll);
	 
	}

	

	public UserTransfer() {

		JLabel labelAmount = new JLabel("Enter Amount");

		 textfieldAmount = new JTextField("");

		JLabel labelConfirm = new JLabel("Enter Password to Confirm");

		passwordfieldConfirm = new JPasswordField("");

		buttonConfirm = new JButton("Confirm");
		buttonConfirm.setActionCommand("TransferConfirm");

		add(passwordfieldConfirm);
		add(buttonConfirm);
		add(labelConfirm);
		add(textfieldAmount);
		add(labelAmount);

		
		setBackground(Color.decode("#FFFFFF"));
		setLayout(null);
		
	    ImageIcon adminPanelGradient = new ImageIcon("gfx/adminPanelGradient.png");

		JLabel topPanel = new JLabel(adminPanelGradient);	
		topPanel.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.decode("#CCCCCC")));
		
		
		JLabel panelTitle = new JLabel("Transfer");
		panelTitle.setFont(new Font("Helvetica Neue", Font.PLAIN, 28));
		
		JLabel activityLabel = new JLabel("Find Recipient");
		activityLabel.setFont(new Font("Arial", Font.PLAIN, 13));
		
				
	    searchField = new JTextField();
		searchField.setFont(new Font("Arial", Font.PLAIN, 13));
		
		
		JButton searchButton = new JButton("Search");
		searchButton.setFont(new Font("Arial", Font.PLAIN, 13));
		searchButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				//HER SKAL VI FINDE UD AF HVORDAN VI KAN FAA VAERDIEN AF searchField IND I FUNKTIONEN NEDENUNDER.
				UserSearch(searchField.getText());
				
			}
		});
		
		//Setting Bounds
		searchButton.setBounds(539, 150, 100, 30);
		searchField.setBounds(10, 120, 630, 30);
		activityLabel.setBounds(10, 100, 630, 20);
		topPanel.setBounds(0, 0, 650, 80);
		buttonConfirm.setBounds(10, 485, 120, 30);
		panelTitle.setBounds(10, 35, 650, 50);
		labelAmount.setBounds(10, 373, 200, 30);
		textfieldAmount.setBounds(10, 395, 120, 30);
		passwordfieldConfirm.setBounds(10, 445, 220, 30);
		labelConfirm.setBounds(10, 423, 200, 30);

		//UserSearch("");
		
		// ADDING ELEMENTS
		add(searchField);
		add(searchButton);
		add(panelTitle);
		add(topPanel);
		add(activityLabel);
	}

	public void addACList(ActionListener ac){
		buttonConfirm.addActionListener(ac);
		
		
	}
	public JPasswordField getPasswordfieldConfirm() {
		return passwordfieldConfirm;
	}

	public JTextField getTextfieldAmount() {
		return textfieldAmount;
	}
}
